import { CustomStrategy, PassportStrategy } from '@midwayjs/passport';
import { Strategy } from 'passport-local';
import { Repository } from 'typeorm';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { User } from '../../../infrastructure/entity/user.entity';
// import * as bcrypt from 'bcrypt';

@CustomStrategy()
export class LocalStrategy extends PassportStrategy(Strategy) {

  @InjectEntityModel(User)
  userModel: Repository<User>;

  // 策略的验证
  async validate(username, password) {
    console.log(username, password);
    const user = await this.userModel.findOneBy({ username });
    console.log(user);
    // if (await bcrypt.compare(password, user.password)) {
    //   throw new Error('error password ' + username);
    // }

    return {
      username,
      password,
    };
  }

  // 当前策略的构造器参数
  getStrategyOptions(): any {
    return {};
  }
}